package com.warmlight.voicepacket.adapter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;

import com.warmlight.voicepacket.data.AllplayerData;
import com.warmlight.voicepacket.data.eventbus.ShareUnlock;
import com.warmlight.voicepacket.fragment.CoverListFragment;

import java.util.List;

/**
 * 全部播单fragment的viewpager adapter
 */
public class AllPlayerCoverAdapter extends FragmentPagerAdapter {
    private List<AllplayerData.DataBean.CategoryBean> category;
    private FragmentManager fragmentManager;
    private String type;
    private String pushtitle;
    public AllPlayerCoverAdapter(FragmentManager fm, List<AllplayerData.DataBean.CategoryBean> category,String type,String pushtitle) {
        super(fm);
        this.fragmentManager = fm;
        this.category = category;
        this.type = type;
        this.pushtitle = pushtitle;
    }

    @Override
    public Fragment getItem(int position) {
        AllplayerData.DataBean.CategoryBean categoryBean = category.get(position);
        CoverListFragment coverListFragment = new CoverListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("category_id",categoryBean.getId());
        bundle.putString("type",type);
        bundle.putString("pushtitle",pushtitle);
        coverListFragment.setArguments(bundle);
        return coverListFragment;
    }

    @Override
    public int getCount() {
        return category.size();
    }


    @Override
    public int getItemPosition(Object object) {
        return super.getItemPosition(object);
    }

    @Override
    public CharSequence getPageTitle(int position) {
//        return super.getPageTitle(position);
        AllplayerData.DataBean.CategoryBean categoryBean = category.get(position);
        return categoryBean.getName();
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        return super.instantiateItem(container, position);
    }

    public void refresh(ViewPager vp_cover, ShareUnlock messageEvent) {
        String cover_id = messageEvent.getCover_id();
        int item = messageEvent.getItem();

        if (item == 0){
            String tag = "android:switcher:" + vp_cover.getId() + ":" + 1;
            CoverListFragment fragmentByTag = (CoverListFragment) fragmentManager.findFragmentByTag(tag);
            if (fragmentByTag != null){
                fragmentByTag.refresh(cover_id);
            }
        }else if (item == 1){
            String tag = "android:switcher:" + vp_cover.getId() + ":" + 0;
            CoverListFragment fragmentByTag = (CoverListFragment) fragmentManager.findFragmentByTag(tag);
            if (fragmentByTag != null){
                fragmentByTag.refresh(cover_id);
            }
        }

    }
}
